//package com.touchfired.common.event.handler.impl;
//
//import com.touchfired.common.constants.ClientInfo;
//import com.touchfired.common.constants.EventCode;
//import com.touchfired.common.constants.GlobalConstants;
//import com.touchfired.common.constants.RoomInfo;
//import com.touchfired.common.context.ServerContext;
//import com.touchfired.common.exception.ErrorCode;
//import com.touchfired.common.exception.ServiceException;
//import com.touchfired.common.message.BaseMessage;
//import com.touchfired.common.message.transfer.MessageSender;
//import io.netty.channel.ChannelHandlerContext;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.util.List;
//import java.util.Optional;
//
///**
// * @Description 加入房间事件码处理器
// * @Author 幸佳俊
// */
//@Component
//public class EventCodeRoomJoinHandler extends BaseEventCodeHandler {
//
//    @Autowired
//    private ServerContext serverContext;
//
//    public EventCodeRoomJoinHandler() {
//        super(EventCode.ROOM_JOIN);
//    }
//
//    @Override
//    public EventCodeHandler doHandle(ChannelHandlerContext ctx, ClientInfo clientInfo, BaseMessage message) throws Exception {
//        // 房间信息
//        List<RoomInfo> rooms = serverContext.getRooms();
//        StringBuilder roomListResult = new StringBuilder();
//        rooms.forEach(roomInfo -> {
//            roomListResult.append(roomInfo.getRoomId()).append("、").append(roomInfo.getRoomName())
//                    .append("（")
//                    .append(roomInfo.getRoomClients().size())
//                    .append("/")
//                    .append(roomInfo.getGameRule().getPlayerNum())
//                    .append("）")
//                    .append("\n");
//        });
//
//        // 逻辑处理
//        try {
//            String content = message.getContent();
//            if (StringUtils.isNotBlank(content)) {
//                if (GlobalConstants.BACK.equalsIgnoreCase(content)) {
//                    return eventHandle(ctx, clientInfo, message, EventCode.PADDING_JOINING);
//                }
//                Optional<RoomInfo> selectRoomInfo = rooms.stream().filter(r -> r.getRoomId().equals(content)).findFirst();
//                if (selectRoomInfo.isPresent()) {
//                    RoomInfo roomInfo = selectRoomInfo.get();
//                    roomInfo.getRoomClients().add(clientInfo.getLongId());
//                    clientInfo.setRoomId(content);
//                    return eventHandle(ctx, clientInfo, message, EventCode.ROOM_PADDING);
//                } else {
//                    throw new ServiceException(ErrorCode.ROOM_JOIN_ID_NULL);
//                }
//            }
//        } catch (ServiceException se) {
//            MessageSender.error(ctx.channel(), message, se.getDesc());
//        } catch (NumberFormatException ne) {
//            MessageSender.error(ctx.channel(), message, ErrorCode.ERROR_OPTIONS_INPUT_VALUE.getDesc());
//        }
//
//        // 有房间
//        if (rooms.size() > 0) {
//            String joinTips = "\n" +
//                    roomListResult.toString() + "\n" +
//                    "请输入房间号加入\n" +
//                    "[注]输入‘" + GlobalConstants.BACK + "’回退上一级。";
//            MessageSender.info(ctx.channel(), message, joinTips);
//        }
//
//        // 无房间
//        else {
//            message.setContent(null);
//            MessageSender.info(ctx.channel(), message, "暂无房间");
//            return eventHandle(ctx, clientInfo, message, EventCode.PADDING_JOINING);
//        }
//        return this;
//    }
//}
